import '../http_exceptions.dart';
import 'response_data.dart';
part 'response_model_analysis.dart';

class ResponseModel<T> {
  AppException? exception;

  //1000成功 1001失败  1002需要登录 1004 参数缺失 -1系统异常 -2 服务繁忙
  int? code;
  String? message;
  T? data;

  int? page;
  int? totalPage;
  int? total;

  bool get isSucceed => (code == ResponseCode.requestSuccessful);

  ResponseModel(
    this.data,
    this.code,
    this.message, {
    this.page,
    this.totalPage,
    this.total,
  });

  //报错
  ResponseModel.err(this.exception);

  //无数据
  ResponseModel.noDataSucceed(Map<String, dynamic>? json) {
    code = (json?[ResponseData.errorCode] ?? -1) as int;
    message = (json?[ResponseData.errorMsg] ?? "") as String;
    data = json?[ResponseData.data];
  }

  //解析{}
  factory ResponseModel.fromJson(Map<String, dynamic>? json,
          T Function(Map<String, dynamic>? json) fromJsonT) =>
      _$ResponseModelFromJson(json, fromJsonT);

  //解析[]
  factory ResponseModel.fromListJson(Map<String, dynamic>? json,
          T Function(List<dynamic>? json) fromJsonT) =>
      _$ResponseListModelFromJson(json, fromJsonT);

  ResponseModel.fromString(
    String? json,
  ) {
    data = json as T?;
  }

  Map<String, dynamic> toJson(Object Function(T? value) toJsonT) =>
      _$ResponseModelToJson(this, toJsonT);
}
